<template>
  <div v-if="isAddress">
    <div class="shopcart-container">
      <!-- 收货地址区域 -->
      <div class="address-container">
        <div class="card">
          <h3>
            {{ this.shopcart_orderlist.order_address.name }}
            {{ this.shopcart_orderlist.order_address.tel }}
          </h3>
          <br />
          <p>{{ this.shopcart_orderlist.order_address.address }}</p>
        </div>
      </div>

      <!-- 商品列表 -->
      <div class="goods-list">
        <!-- 商品列表项区域 -->

        <div class="card">
          <div
            class="card-content"
            v-for="item in shopcart_orderlist.goods_list"
            :key="item.goods_id"
          >
            <div class="card-content-inner flex">
              <img :src="item.goods_small_logo" />
              <div class="info">
                <h1>{{ item.goods_name }}</h1>
                <p class="flex">
                  <span class="price">¥{{ item.goods_price }}</span>
                  <span>x{{ item.num }}</span>
                </p>
              </div>
            </div>
          </div>

          <!-- 配送信息 -->
          <div class="process-info">
            <p>
              <strong>配送服务</strong>
              <strong>快递运输</strong>
            </p>
            <br />
            <p>
              <span>中小件送货时间</span>
              <span>工作日、双休日与节假日均可送货</span>
            </p>
          </div>
          <!-- 订单备注 -->
          <div class="store-info">
            <strong>订单备注</strong><br /><br />
            <textarea
              v-model="note"
              type="text"
              placeholder="选填，给商家留言"
            ></textarea>
          </div>
        </div>
      </div>
      <!-- 运费信息 -->
      <ul class="fare-info card">
        <li class="fare-price flex">
          <span>商品金额</span>
          <span class="red">¥{{ this.shopcart_orderlist.order_price }}</span>
        </li>
        <li class="fare-price flex">
          <span>运费</span>
          <span class="red">¥0.00</span>
        </li>
        <li class="fare-price flex">
          <span><strong>总价</strong></span>
          <span class="red">¥{{ this.shopcart_orderlist.order_price }}</span>
        </li>
        <!-- 创建订单按钮 -->
        <div class="order_button">
          <van-button type="info" @click="showPopup()">更换地址</van-button>
          <van-button type="info" @click="create()">创建订单</van-button>
        </div>
      </ul>

      <!-- 弹出地址选择 -->
      <van-popup v-model="show" closeable position="bottom">
        <div>
          <div
            class="address-card"
            @click="Change_address(i)"
            v-for="(item, i) in this.address"
            :key="i"
          >
            <div class="top">{{ item.name }}&nbsp;{{ item.tel }}</div>
            <div class="bottom">{{ item.address }}</div>
          </div>
        </div>
      </van-popup>
    </div>
  </div>
</template>

<script>
import { mapGetters, mapMutations, mapState } from "vuex";

export default {
  data() {
    return {
      address_index: 0,
      shopcart_orderlist: {},
      note: "",
      show: false,
      chosenAddressId: "1",
      isShopFrom: false,
      isAddress: true,
    };
  },
  computed: {
    ...mapGetters("order", ["getLength"]),
    ...mapGetters("address", ["getAddress"]),
    ...mapMutations("order", ["AddOrder"]),
    ...mapState("address", ["address"]),
  },
  created() {
    if (this.getAddress == "") {
      this.isAddress = false;
      this.$router.replace({
        name: "address_add",
      });
    }
    this.shopcart_orderlist.order_address = this.getAddress[0];
    this.shopcart_orderlist = this.$route.params.shopcart_orderlist;
  },
  methods: {
    Change_address(i) {
      this.shopcart_orderlist.order_address = this.getAddress[i];
      this.show = false;
    },
    showPopup() {
      this.show = true;
    },
    create() {
      this.shopcart_orderlist.order_id = this.getLength;
      this.shopcart_orderlist.note = this.note;
      console.log(this.getLength);
      this.$store.commit("order/AddOrder", this.shopcart_orderlist);
      if (this.isShopFrom) {
        this.$store.commit("shopcart/clearCart");
      }
      this.$router.replace({
        name: "order_list",
      });
    },
    change_address() {},
  },
  activated() {
    let index = this.$route.params.index ? this.$route.params.index : 0;
    this.shopcart_orderlist.order_address = this.getAddress[index];
  },
  beforeRouteEnter(to, from, next) {
    next((vm) => {
      if (from.path == "/shopcaty") {
        vm.isShopFrom = true;
      }
    });
  },
};
</script>
<style lang="scss" scoped>
.flex {
  display: flex;
}
.order_button {
  text-align: center;
  display: flex;
  justify-content: space-around;
}
.shopcart-container {
  overflow: hidden;
  // 收货地址
  .address-container {
    border-bottom: 2px solid #eee;

    .card {
      margin: 0;
      padding: 15px;
      h3 {
        color: #333;
        font-size: 16px;
        font-weight: bold;
      }
    }
  }

  // 商品列表
  .goods-list {
    margin-top: 10px;
    .card {
      margin: 0;
      .card-content {
        border-bottom: 1px solid #eee;
        .card-content-inner {
          align-items: center;
          padding: 15px;
          img {
            width: 60px;
          }
          .info {
            margin-left: 10px;
            box-sizing: border-box;
            width: 100%;
            overflow: hidden;
            h1 {
              font-size: 13px;
              font-weight: bold;
              line-height: 20px;
              padding-top: 22px;
            }
            p {
              flex-direction: row;
              align-items: center;
              justify-content: space-between;
              .price {
                font-size: 16px;
                font-weight: 700;
                color: red;
                flex: 1;
              }
            }
          }
        }
      }
      // 配送信息
      .process-info {
        padding: 15px;
        border-bottom: 1px solid #eee;
        p {
          display: flex;
          flex-direction: row;
          align-items: center;
          justify-content: space-between;
          color: #333;
          span {
            color: #999;
          }
        }
      }
      // 店铺备注
      .store-info {
        padding: 10px;
        textarea {
          font-size: 13px;
          width: 100%;
          height: 50px;
        }
      }
    }
  }
  // 运费信息
  .fare-info {
    padding: 15px;
    margin: 10px 0 0 0;
    .fare-price {
      padding: 5px 0;
      display: flex;
      justify-content: space-between;
      .red {
        color: red;
        font-size: 16px;
      }
    }
  }
}
.address-card {
  padding: 40px 15px 15px 15px;
  border-bottom: 1px solid #000;
  .top {
    margin-bottom: 5px;
  }
}
</style>
